package com.hairline.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hairline.bean.Coupon;
import com.hairline.bean.CouponExample;
import com.hairline.bean.CouponUser;
import com.hairline.bean.vo.AllCoupon;
import com.hairline.bean.vo.MyCoupon;
import com.hairline.mapper.CouponMapper;
import com.hairline.mapper.CouponUserMapper;
import com.hairline.mapper.MyCouponMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class WxCouponServiceImpl implements WxCouponService{
    @Autowired
    CouponMapper couponMapper;

    @Autowired
    MyCouponMapper myCouponMapper;

    @Autowired
    CouponUserMapper couponUserMapper;

    @Override
    public Map<String,Object> getAllCoupon(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<AllCoupon> couponList = myCouponMapper.getAllCoupon();
        PageInfo<AllCoupon> couponPageInfo = new PageInfo<>(couponList);
        long total = couponPageInfo.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",couponList);
        map.put("count",total);
        return map;
    }

    @Override
    public Map<String,Object> getMyCoupon(Integer userId, Integer status, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<MyCoupon> couponList = myCouponMapper.getMyCoupon(userId,status);
        PageInfo<MyCoupon> couponPageInfo = new PageInfo<>(couponList);
        long total = couponPageInfo.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",couponList);
        map.put("count",total);
        return map;
    }

    @Override
    public List<MyCoupon> selectMyCoupon(Integer userId) {
        return myCouponMapper.selectMyCoupon(userId);
    }

    @Override
    public Coupon selectCoupon(Integer couponId) {
        return couponMapper.selectByPrimaryKey(couponId);
    }

    @Override
    public Integer selectCouponUser(Integer userId, Integer couponId) {
        return myCouponMapper.selectCouponUser(userId,couponId);
    }

    @Override
    @Transactional
    public void addUserCoupon(Integer userId, Coupon coupon) {
        CouponUser couponUser = new CouponUser();
        couponUser.setUserId(userId);
        couponUser.setCouponId(coupon.getId());
        couponUser.setStatus((short) 0);
        Date time = new Date();
        if (coupon.getDays() != null){
            couponUser.setStartTime(time);
            Calendar c = Calendar.getInstance();
            c.setTime(time);
            c.add(Calendar.DAY_OF_MONTH, coupon.getDays());
            if (coupon.getEndTime() != null){
                if (c.getTime().compareTo(coupon.getEndTime()) > 0){
                    couponUser.setEndTime(coupon.getEndTime());
                }else {
                    couponUser.setEndTime(c.getTime());
                }
            }else {
                couponUser.setEndTime(c.getTime());
            }
        }else {
            couponUser.setStartTime(coupon.getStartTime());
            couponUser.setEndTime(coupon.getEndTime());
        }
        couponUser.setAddTime(time);
        couponUser.setDeleted(false);
        couponUserMapper.insert(couponUser);
        cutCoupon( coupon);
    }

    private void cutCoupon(Coupon coupon) {
        if (coupon.getTotal().intValue() == 0){
            return;
        }

        if (coupon.getTotal().intValue() == 1){
            coupon.setTotal(-1);
        }else {
            coupon.setTotal(coupon.getTotal() -1);
        }

        couponMapper.updateByPrimaryKeySelective(coupon);
        return;

    }

    @Override
    public Coupon getCouponByCode(String code) {
        CouponExample couponExample = new CouponExample();
        couponExample.createCriteria().andCodeEqualTo(code);
        List<Coupon> couponList = couponMapper.selectByExample(couponExample);
        if (couponList.size() == 0){
            return null;
        }
        return couponList.get(0);
    }
}
